var Mock = require('mockjs');
// 配置mock
Mock.setup({
    timeout: "300-600", // 延迟 300 毫秒 到 600毫秒返回数据
});
let users1 = [];
for(var i=0 ; i<10 ; i++){
    users1.push(Mock.mock({
        id:'@guid',
        username:'@first',
        'password | 11':/^[a-zA-Z]\d{10}$/
    }))
}
let user2 = [{
        id:1545454545,
        username:'laowu',
        password:'laowu123456'
    },
    {
        id:15454545,
        username:'laoliu',
        password:'laoliu123456'
    }]
let users = users1.concat(user2)
export const getUserList =() => {
    if(!localStorage.getItem("users")){
        localStorage.setItem("users",JSON.stringify(users))
    }
    return localStorage.getItem("user")
}
export const loginHandler = (options) => {
    let getUsers = JSON.parse(localStorage.getItem("users")) 
    console.log(getUsers);
    let user = getUsers.filter(item => {
        if(item.username == JSON.parse(options.body).username){
            return item
        }  
    })
    console.log(user,'匹配用户信息');
    if(user.length){
        console.log(user[0].password,'用户密码')
        if(user[0].password == JSON.parse(options.body).password){
            return {
                code:'200',
                data:{
                    username: JSON.parse(options.body).username,
                    token:'obsahsasa1545484545451515'
                }
            }
        }else{
            return {
                code:'1001',
                msg:'密码错误'
            } 
        }
        
    }else{
        return {
            code:'1002',
            msg:'账号错误'
        }  
    }

    // for(var index in getUsers){
    //     console.log(getUsers,'用户');
    //     console.log(getUsers[index],'账户密码');
    //     if(getUsers[index].username == options.body.username){
    //         if(getUsers[index].password == options.body.password){
    //             return {
    //                 code:'200',
    //                 username: options.body.username,
    //                 data:{
    //                     token:'obsahsasa1545484545451515'
    //                 }
    //             } 
    //         }else{
    //             console.log(1212121212);
    //             return {
    //                 code:'1001',
    //                 msg:'密码错误'
    //             } 
    //         }
    //     }
    // }

    
}



// 拦截请求
Mock.mock("/getlist", "get", getUserList)
Mock.mock("/login", "post", loginHandler)

